﻿using OneStationFile.IBLL;
using OneStationFile.Model;
using OneStationFile.WEB.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace OneStationFile.WEB.Controllers
{
  
    public class UsersController : Controller
    {
        protected IBaseService BaseService { get; set; }
        // GET: Users
        #region index，列表。
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="kwName"></param>
        /// <returns></returns>
        public ActionResult Index(int id = 0, int page = 1, string kwName = "")
        {
            List<Model.Users> queryOver = BaseService.GetModels<Model.Users>().List().ToList();
            if (!string.IsNullOrEmpty(kwName))
            {
                queryOver = queryOver.Where(p => p.Name.Contains(kwName)).ToList();
            }
            PagedList<Model.Users> plm = queryOver.ToPagedList(page, 8);
            /// PagedList<Model.Users> plm = bmu.GetListByPage(Where, "id desc ", page, 10).ToPagedList(page, 10);
            plm.CurrentPageIndex = page;
            plm.TotalItemCount = queryOver.Count();//总记录数
            return View(plm);
        }
        #endregion

        #region role  设置权限
        public ActionResult Role(int id = 0, int page = 1)
        {
            Model.Users users = BaseService.GetModelById<Model.Users>(id);
            ViewBag.usersRole = users.Role;
            List<Role> roles = BaseService.GetModels<Role>().List().ToList();
            PagedList<Model.Role> plm = roles.ToPagedList(page, 8);
            plm.CurrentPageIndex = page;//当前页数
            plm.TotalItemCount = roles.Count();//总记录数
            return View(plm);
        }
        #endregion

        #region AddRole 添加角色
        /// <summary>
        /// AddRole 添加角色
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult AddRole(int roleId,int id)
        {
            object result = new { msg = "添加成功！", res = true };
            if (roleId<= 0 || id<=0)
            {
                result = new { msg = "参数不能为空！", res = false };
                return Json(result);
            }
            Model.Role role = BaseService.GetModelById<Model.Role>(roleId);
            Model.Users users = BaseService.GetModelById<Model.Users>(id);
            if (role == null || users == null)
            {
                result = new { msg = "部分数据不完整，请稍后再试！", res = false };
                return Json(result);
            }
            users.Role = role;
            role.Users.Add(users);
            BaseService.SaveOrUpdate(users);
            BaseService.SaveOrUpdate(role);
            return Json(result);
        }
        #endregion

        #region edit 编辑页面
        /// <summary>
        /// edit 编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        
        public ActionResult Edit(int id = 0)
        {
            if (id > 0)
            {
                ViewBag.action = "修改";
                Model.Users Users = BaseService.GetModelById<Model.Users>(id);
                return View(Users);
            }
            ViewBag.action = "添加";
            return View();
        }
        #endregion

        #region Delete 删除（ajax）
        /// <summary>
        /// 删除（ajax）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        
        public JsonResult Delete(int id)
        {
            var result = new { msg = "删除成功!", res = true };
            if (id > 0)
            {
                if (!BaseService.DeleteModelById<Model.Users>(id))
                {
                    result = new { msg = "删除失败!", res = false };
                    return Json(result);
                }

            }
            return Json(result);
        }
        #endregion

        #region edithandle 添加或修改(ajax)
        [HttpPost]
        public JsonResult EditHandle(Model.Users model, FormCollection collection)
        {
            var result = new { msg = "操作成功!", res = true };
            Model.Users Users = null;
            if (string.IsNullOrEmpty(model.Name))
            {
                result = new { msg = "名称不能为空", res = false };
                return Json(result);
            }
            if (string.IsNullOrEmpty(model.Account))
            {
                result = new { msg = "登录名不能为空", res = false };
                return Json(result);
            }

            if (model.Id > 0)
            {
                if (BaseService.ExistModelByWhere<Model.Users>(p => p.Name == model.Name) >= 2)
                {
                    result = new { msg = "名称已经存在！", res = false };
                    return Json(result);
                }
                Users = BaseService.GetModelById<Model.Users>(model.Id);
                try
                {
                    var role = collection.GetValue("Role.Id");

                    UpdateModel(Users);
                }
                catch (Exception ex)
                {

                    throw (ex);
                }
            }
            else
            {
                if (BaseService.ExistModelByWhere<Model.Users>(p => p.Name == model.Name) >= 1)
                {
                    result = new { msg = "名称已经存在！", res = false };
                    return Json(result);
                }
                Users = model;
                //Users.Nodes = null;
            }
            if (BaseService.SaveOrUpdate(Users) == false)
            {
                result = new { msg = "保存时，出现在错误", res = false };
                return Json(result);
            }
            return Json(result);
        }
        #endregion
    }

}